package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.Integral;
import com.applet.mallappletbackstage.service.IntegralService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Integral)表控制层
 *
 * @author makejava
 * @since 2020-08-14 16:40:46
 */
@RestController
@RequestMapping("/integral")
public class IntegralController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IntegralService integralService;

    @GetMapping("states/{user}")
    public R getStates(@PathVariable String user) {
        Integral one = integralService.getStates(user);
        return R.success(one);
    }

    /**
     * 分页查询所有数据
     *
     * @param current
     * @return 所有数据
     */
    @GetMapping("selectAll/{current}")
    public R selectAll(@PathVariable long current
            , @RequestParam(required = false) String user
            , @RequestParam(required = false) String category
            , @RequestParam(required = false) String origin) {
        Page<Integral> integralPage = new Page<>(current, 15);
        QueryWrapper<Integral> integralQueryWrapper = new QueryWrapper<>();
        if (user != null) {
            integralQueryWrapper.eq("user", user);
        }
        if (category != null && !"".equals(category)) {
            integralQueryWrapper.eq("category", category);
        }
        if (origin != null && !"".equals(origin)) {
            integralQueryWrapper.eq("origin", origin);
        }
        Page<Integral> integrals = integralService.page(integralPage, integralQueryWrapper);
        return R.success(integrals);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("sum/{id}")
    public R selectSum(@PathVariable String id) {
        Integer sum = integralService.selectSum(id);
        return R.success(sum);
    }

    /**
     * 新增数据
     *
     * @param integral 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Integral integral) {
        return R.success(this.integralService.save(integral));
    }

    /**
     * 修改数据
     *
     * @param integral 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Integral integral) {
        return R.success(this.integralService.updateById(integral));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.integralService.removeByIds(idList));
    }

    /***
     * 用户授权注册新增积分
     * @param user
     * @return
     */
    @PostMapping("/addIntegral/{user}")
    public R addIntegral(@PathVariable(value = "user")String user){
        integralService.addIntegral(user);
        return R.success("添加积分成功");
    }
}
